import math
def qvalue(p,n):
    return p**2/(n*n+1)
total_p = 200

p = [51,99,50]
n_total = 1
q_table = []
while n_total < sum(p):
    basen = [math.ceil(i/sum(p)*n_total) for i in p]
    while sum(basen) < n_total:
        q_table = []
        for pair in zip(p,basen):
            q_table.append(qvalue(pair[0],pair[1]))
        basen[q_table.index(max(q_table))] += 1
    q_min = []
    q = []
    q_max = []
    for i in p:
        temp_n = (i / sum(p)) * n_total
        q.append(temp_n)
        q_min.append(int(temp_n))
        q_max.append(math.ceil(temp_n))
    print(n_total)
    #[q_table.index(max(q_table))] += 1
    flag = 0
    for i in range(len(basen)):
        if q_min[i] <= basen[i] <= q_max[i] :
            pass
        else:
            flag = 1
    if flag == 1:
        break
    n_total +=1
print(q_table)
print(p)
print(basen)
print(n_total)
print(q_min)
print(q)
print(q_max)